<?php

namespace Admin\Controller;

use Think\Controller;

class FeedbackController extends CommonController
{
    /**
     * 反馈页
     */
    public function feedback()
    {
        $feedbackModel = M('user_feedback', '', 'KEFU');
        $status = I('status');
        $where['b.type'] = 1;
        if (!empty($status)) {
            if ($status == 3 || $status == '3') {//未完结
                $where['b.status'] = array('neq', 3);
            } else {
                $where['b.status'] = $status;
            }
        } else {
            $where['b.status'] = 1;//未回复
        }
        $count = $feedbackModel->join('b join dxd_user.user a on b.mobile=a.mobile and a.type=1')->where($where)->count();
        $field = 'b.content,b.id,b.mobile,b.ctime,b.status,b.type,b.upd_time,b.vid,a.truename as username,a.id as user_id';
        $data = $feedbackModel->join('b join dxd_user.user a on b.mobile=a.mobile and a.type=1 ')->where($where)->LIMIT(0, 10)->field($field)->order('b.upd_time desc')->select();

        foreach ($data as $k => $v) {
            //回复数
            $data[$k]['back_num'] = M('user_message', '', 'KEFU')->where(['uf_id' => $v['id'], 'vid' => '0'])->count();
            $user_message = M('user_message', '', 'KEFU')->where(['uf_id' => $v['id'], 'vid' => '0'])->field('ctime')->order('ctime desc')->find();
            if (!empty($user_message['ctime'])) {//最近回复时间
                $data[$k]['back_time'] = date('Y-m-d H:i:s', $user_message['ctime']);
            } else {
                $data[$k]['back_time'] = "--:--:--";
            }

            if(strpos($v['content'],'/')!==false || strstr($v['content'],'>')){
                $data[$k]['content']=substr($v['content'],0,5);
            }

            //回复状态
            switch ($v['status']) {
                case 1:
                    $data[$k]['status'] = '未回复';
                    break;
                case 2:
                    $data[$k]['status'] = '已回复';
                    break;
                case 3:
                    $data[$k]['status'] = '完成';
                    break;
            }

            if (empty($v['username'])) {
                $data[$k]['username'] = "-----";
            }
            //回复方式
            if ($v['type'] == '1') {
                $data[$k]['type'] = '短信反馈';
            } else if ($v['type'] == '2') {
                $data[$k]['type'] = '电话反馈';
            }
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
        }
        $tol = ceil($count / 10);
        $this->assign(array(
            'data' => $data,
            'tol' => $tol,
            'count' => $count,
        ));
        $this->display();
    }

    /**
     * 获取最新的反馈数量
     */
    public  function feedback_new(){
//        $time=time()-60*5;
        $num=M('user_feedback','','KEFU')->where(['status'=>1])->count();
        echo $num;
    }

    /**
     *  用户反馈
     */
    public function kf_user_list_feedback()
    {
        $n = I('page', 1);
        $num = 10;
        $page = ($n - 1) * $num;
        $feedbackModel = M('user_feedback', '', 'KEFU');
        $search = I('feedback');
        $status = I('status');
        $where['b.type'] = 1;
        if (!empty($search)) {
            $where['b.content|b.mobile|b.id|a.truename|b.ctime'] = array('like', '%' . $search . '%');
        }

        if (!empty($status)) {
            if ($status == 3 || $status == '3') {//未完结
                $where['b.status'] = array('neq', 3);
            } else if ($status == 4) {
                $where['b.status'] = 1;
                $where['b.ctime'] = array('lt', time() - 1 * 3600);
            } else {
                $where['b.status'] = $status;
            }
        }
        $count = $feedbackModel->join('b join dxd_user.user a on b.mobile=a.mobile ')->where($where)->count();
        $field = 'b.content,b.id,b.mobile,b.ctime,b.status,b.type,b.upd_time,b.vid,a.truename as username, a.id as user_id';
        $data = $feedbackModel->join('b join dxd_user.user a on b.mobile=a.mobile')->where($where)->LIMIT($page, $num)->field($field)->order('b.ctime desc,b.status asc,b.id desc')->select();
        foreach ($data as $k => $v) {
            $data[$k]['back_num'] = M('user_message', '', 'KEFU')->where(['uf_id' => $v['id'], 'vid' => '0'])->count();
            $user_message = M('user_message', '', 'KEFU')->where(['uf_id' => $v['id'], 'vid' => '0'])->field('ctime')->order('ctime desc')->find();
            if (!empty($user_message['ctime'])) {//最近回复时间
                $data[$k]['back_time'] = date('Y-m-d H:i:s', $user_message['ctime']);
            } else {
                $data[$k]['back_time'] = "--:--:--";
            }
            if(strpos($v['content'],'/')!==false || strstr($v['content'],'>')){
                $data[$k]['content']=substr($v['content'],0,5);
            }
            //回复状态
            switch ($v['status']) {
                case 1:
                    $data[$k]['status'] = '未回复';
                    break;
                case 2:
                    $data[$k]['status'] = '已回复';
                    break;
                case 3:
                    $data[$k]['status'] = '完成';
                    break;
            }
            if (empty($v['username'])) {
                $data[$k]['username'] = "----";
            }
            //回复方式
            if ($v['type'] == '1') {
                $data[$k]['type'] = '短信反馈';
            } else if ($v['type'] == '2') {
                $data[$k]['type'] = '电话反馈';
            }
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
//            $data[$k]['back_time'] = date('Y-m-d H:i:s', $v['upd_time']);
        }
        $tol = ceil($count / $num);//页数
        $count = intval($count);
        $this->ajaxReturn(['code' => '1001', 'tol' => $tol, 'count' => $count, 'result' => $data]);

    }

    /**
     *  反馈详情
     */
    public function feedback_detail()
    {
        $id = I('id');
        //用户反馈
        $user_feedback = M('user_feedback', '', 'KEFU')->join('a join dxd_user.user b on a.mobile=b.mobile')->where(['a.id' => $id])->field('a.vid,a.content,a.img,a.ctime,a.mobile,a.upd_time,a.status,b.truename')->find();

        if(strpos($user_feedback['content'],'/')!==false || strstr($user_feedback['content'],'>')){
            $user_feedback['content']=substr($user_feedback['content'],0,5);
        }

        $user_feedback['ctime'] = date('Y-m-d H:i:s', $user_feedback['ctime']);

        $first=substr($user_feedback['mobile'],0,3);
        $end=substr($user_feedback['mobile'],7,4);
        $user_feedback['user_mobile']=$first.'****'.$end;
        //回复数
        $user_feedback['back_num'] = M('user_message', '', 'KEFU')->where(['uf_id' => $id, 'vid' => '0'])->count();
        $user_message = M('user_message', '', 'KEFU')->where(['uf_id' => $id, 'vid' => '0'])->field('ctime')->order('ctime desc')->find();
        if (!empty($user_message['ctime'])) {//最近回复时间
            $user_feedback['update_time'] = date('Y-m-d H:i:s', $user_message['ctime']);
        } else {
            $user_feedback['update_time'] = "--:--:--";
        }
        if (!empty($user_feedback['upd_time'])) {
            $user_feedback['upd_time'] = date('Y-m-d H:i:s', $user_feedback['upd_time']);
        } else {
            $user_feedback['upd_time'] = "--";
        }
        if (empty($user_feedback['truename'])) {
            $user_feedback['truename'] = '--';
        }

        //历史沟通
        $history_feedback = M('user_message', '', 'KEFU')->where(['uf_id' => $id])->field('id,vid,img,content,admin_name,ctime')->order('ctime desc')->select();
        foreach ($history_feedback as $k => $v) {
            $history_feedback[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            $admin_id = M('admin', '', 'KEFU')->where(['name' => $v['admin_name']])->getField('id');
            $history_feedback[$k]['admin_id'] = '0' . $admin_id;
            if (!empty($v['img'])) {
                $history_feedback[$k]['img'] = explode(',', $v['img']);
            } else {
                $history_feedback[$k]['img'] = '';
            }
            if ($v['vid'] != '0') {
                $user = M('user', '', 'USER')->where(['vid' => $v['vid']])->Field('truename,mobile')->find();
                if (!empty($user['truename'])) {
                    $history_feedback[$k]['user_name'] = $user['truename'];
                } else {
                    $history_feedback[$k]['user_name'] = $user['mobile'];
                }
            }
            if($v['admin_name']==$_SESSION['name']){
                $history_feedback[$k]['my_feedback'] = 1;
            }else{
                $history_feedback[$k]['my_feedback'] = 2;
            }
        }

        //回复状态 1未回复2已回复3完成4需回电
        if($user_feedback['status']==1){
            $user_feedback['s_status']="未回复";
        }else if($user_feedback['status']==2){
            $user_feedback['s_status']="已回复";
        }else if($user_feedback['status']==3){
            $user_feedback['s_status']="完成";
        }

        //是否有图片
        $img_arr=explode(',', $user_feedback['img']);
        if(count($img_arr)<1 || empty( $user_feedback['img'])){
            $has_img='2';
        }else{
            $has_img='1';
        }

        $this->assign(array(
            'history_feedback' => $history_feedback,
            'user_feedback' => $user_feedback,
            'vid' => $user_feedback['vid'],
            'uf_id' => $id,
            'feedback_image' =>$img_arr,
            'has_img' =>$has_img,
        ));
        $this->display();

    }

    /**
     *  回复用户
     */
    public function admin_feedback()
    {
        $content = I('content');
        if (empty($content)) {
            $this->ajaxReturn(array('code' => '1002', 'result' => '回复不能为空'));
        }
        $uf_id = I('uf_id');
        $img = I('image');
        $imgurl='';
        if (!empty($img)) {
            $upload = new \Admin\Logic\UploadPicLogic();     //实例化 上传图片
            $picarray = explode('~', $img);
            $url="kefu/workorder/";
            foreach ($picarray as $k1 => $v1) {
                $returnimg = $upload->LoadIgoodimg($v1,$url);
                if ($returnimg['status'] == '1') {
                    if($k1==0){
                        $imgurl=$returnimg['pic'];
                    }else{
                        $imgurl=$imgurl.','. $returnimg['pic'];
                    }
                } else {
                    $this->ajaxReturn(array('code' => '1002','result'=>'图片上传失败'), 'json');
                }
            }
        }

        $arr = array('vid' => '0', 'img' => $imgurl, 'uf_id' => $uf_id, 'content' => $content, 'ctime' => time(), 'admin_name' => $_SESSION['name']);
        $add = M('user_message', '', 'KEFU')->add($arr);
        if ($add) {
            add_log('在线反馈回复'.$add);
            M('user_feedback','','KEFU')->where(['id'=>$uf_id])->setField('status',2);
            $back = M('user_message', '', 'KEFU')->where(['id' => $add])->field('vid,img,content,admin_name,ctime')->select();
            foreach ($back as $k => $v) {
                $back[$k]['ctime'] = date('Y-m-d H:s', $v['ctime']);
                $admin_id = M('admin', '', 'KEFU')->where(['name' => $v['admin_name']])->getField('id');
                $back[$k]['admin_id'] = '0' . $admin_id;
                if (!empty($v['img'])) {
                    $back[$k]['img'] = explode(',', $v['img']);
                } else {
                    $back[$k]['img'] = '';
                }
                if ($v['vid'] != '0') {
                    $user = M('user', '', 'USER')->where(['vid' => $v['vid']])->Field('truename,mobile')->find();
                    if (!empty($user['truename'])) {
                        $back[$k]['user_name'] = $user['truename'];
                    } else {
                        $back[$k]['user_name'] = $user['mobile'];
                    }
                }
            }
            $this->ajaxReturn(array('code' => '1001', 'result' => $back));

        } else {
            $this->ajaxReturn(array('code' => '1001', 'result' => '提交失败'));
        }
    }

    /**
     *上传图片
     */
    public function uploadimg()
    {
        if (empty($_FILES)) {
            $this->ajaxReturn(['code' => '1002', 'result' => '无图']);
        } else {
            $image = $_FILES['uploadimg'];
            $url = 'http://gxxj.oss-cn-hangzhou.aliyuncs.com';
            $success = new \Service\OssClient('LTAI6sZcsEa0SGHc', '053o9lXKyeLh1J1nmlykMg0z8N0Q5E', $url);
            $ext = strtolower(trim(substr($image['name'], strrpos($image['name'], '.')), '.'));

            $image['name'] = md5(time() . uniqid());

            $object = 'adm/' . $image['name'] . '.' . $ext;
            $filePath = $image['tmp_name'];

            try {
                $success->uploadFile('gxxj', $object, $filePath);
                $res = $url . '/adm/' . $image['name'] . '.' . $ext;
                $data['pic'] = $res;
            } catch (Exception $e) {
                printf(__FUNCTION__ . ": FAILED\n");
                printf($e->getMessage() . "\n");
                $res = '1003';
            }

            if ($res == '1002') {
                $data['code'] = '1002';
                $data['result'] = '图片不得大于2M';
            } else {
                $data['code'] = '1001';
                $data['result'] = $res;
            }
            $this->ajaxReturn($data);
        }
    }

    /**
     *  修改反馈内容
     */
    public function edit_feedback()
    {
        $id=I('id');
        $content=I('content');
        $update=M('user_message','','KEFU')->where(['id'=>$id])->setField('content',$content);
         if($update){
             add_log('修改在线回复内容'.$id);
             $this->ajaxReturn(array('code' => '1001', 'result' => "修改成功"));
             die;
         }
        $this->ajaxReturn(array('code' => '1002', 'result' => "修改失败"));
        die;
    }

    /**
     *  任务
     */
    public function task()
    {
        $this->display();
    }


}